package com.guguskins.back.controller;


import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuRoll;
import com.guguskins.back.entity.GuRollRecord;
import com.guguskins.back.entity.GuUser;
import com.guguskins.back.entity.bo.RollEditBo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuRollService;
import com.guguskins.back.utils.DateUtil;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-04
 */
@RestController
@RequestMapping("/roll")
public class GuRollController {

    @Autowired
    private GuRollService rollService;

    @GetMapping("/list")
    public ResultVo getList(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                            @RequestParam(value = "p", required = false, defaultValue = "1") int p,
                            @RequestParam(value = "size", required = false, defaultValue = "6") int size,
                            @RequestParam(value = "state", required = false, defaultValue = "-1") int state) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        PageVo<GuRoll> pageVo = rollService.getList(p, size, userId, state);
        return ResultVo.success(pageVo);
    }

    @GetMapping("/detail/{rollId}")
    public ResultVo detail(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                           @PathVariable(value = "rollId") long rollId) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        GuRoll roll = rollService.getDetail(userId, rollId);
        return ResultVo.success(roll);
    }

    @GetMapping("/participation")
    public ResultVo participation(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                                  @RequestParam(value = "size", required = false, defaultValue = "30") int size,
                                  @RequestParam(value = "rollId") long rollId) {
        PageVo<GuUser> userPageVo = rollService.participation(p, size, rollId);
        return ResultVo.success(userPageVo);
    }

    @PostMapping("/participate")
    @Authorized
    public ResultVo participate(@RequestHeader("token") String token,
                                @RequestBody GuRoll roll) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        boolean result = rollService.participate(userId, roll);
        if (result) {
            return ResultVo.success("参与成功");
        }
        return ResultVo.fail("参与失败");
    }

    @PostMapping("/create")
    public ResultVo create(@RequestHeader("token") String token,
                           @RequestBody GuRoll roll) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        boolean result = rollService.create(userId, roll);
        if (result) {
            return ResultVo.success("创建成功");
        }
        return ResultVo.fail("创建失败");
    }

    @PostMapping("/back/save")
    public ResultVo backSave(@RequestBody GuRoll roll) {

        if (rollService.backSave(roll)) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @GetMapping("/back/{rollId}")
    public ResultVo backGetByRollId(@PathVariable long rollId) {
        return ResultVo.success(rollService.getById(rollId));
    }

    @PutMapping("/back/edit")
    public ResultVo backEdit(@RequestBody RollEditBo rollEditBo) {
        if (rollService.backEdit(rollEditBo)) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @DeleteMapping("/back/{rollId}")
    public ResultVo backDeleteByRollId(@PathVariable long rollId) {
        if (rollService.backDeleteByRollId(rollId)) {
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("删除失败");
    }


    @GetMapping("/back/list")
    public ResultVo backList(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                             @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                             @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        PageVo<GuRoll> list = rollService.backList(p, size, searchKey);
        return ResultVo.success(list);
    }

    @GetMapping("/back/users")
    public ResultVo backUsersByRollId(@RequestParam(value = "rollId") long rollId,
                                      @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        return ResultVo.success(rollService.backUsersByRollId(rollId, searchKey));
    }

    @GetMapping("/back/goods")
    public ResultVo backGoodsByRollId(@RequestParam(value = "rollId") long rollId,
//                                      @RequestParam(value = "p", required = false, defaultValue = "1") int p,
//                                      @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                                      @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        return ResultVo.success(rollService.backGoodsByRollId(rollId, searchKey));
    }

    @PostMapping("/back/prizeAssign")
    public ResultVo backPrizeAssign(@RequestBody GuRollRecord rollRecord) {
        if (rollService.backPrizeAssign(rollRecord)) {
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("操作失败");
    }

    @PostMapping("/back/cancelPrizeAssign")
    public ResultVo backCancelPrizeAssign(@RequestBody GuRollRecord rollRecord) {
        if (rollService.backCancelPrizeAssign(rollRecord)) {
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("操作失败");
    }


}
